package com.minhua.jijiehao2.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.minhua.jijiehao2.R;


/**
 * @author moo
 */
public class LoadingDialog extends Dialog {

    public LoadingDialog(@NonNull Context context) {
        super(context, R.style.LoadingDialog);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.layout_dialog_loading, null);
        initView(view);
        getWindow().setGravity(Gravity.CENTER);
        getWindow().setBackgroundDrawable(new ColorDrawable(0));
        setContentView(view, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        setCanceledOnTouchOutside(false);
    }

    private void initView(View view) {
        ImageView imageView = view.findViewById(R.id.loadingImageView);
        imageView.setImageResource(R.drawable.loading_dialog_anim);
        AnimationDrawable animation = ((AnimationDrawable) imageView.getDrawable());
        animation.start();
    }

    @Override
    public void onBackPressed() {
        cancel();
        if (mOnCancel != null) {
            mOnCancel.onCancel();
        }
    }


    private OnCancel mOnCancel;

    public void setOnCancel(@Nullable OnCancel mOnCancel) {
        this.mOnCancel = mOnCancel;
    }

    public interface OnCancel {
        void onCancel();
    }
}
